﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class RewardDetails : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            if (Session["id"] != null)
            {
                if (Session["Role"].ToString() != "Admin" && Session["Role"].ToString() != "Staff")
                {
                    cmdSearch = new SqlCommand("SELECT * FROM Users WHERE u_id=@uid", conn);
                    cmdSearch.Parameters.AddWithValue("@uid", Session["id"]);
                    dtrSearch = cmdSearch.ExecuteReader();

                    dtrSearch.Read();
                    lblUserID.Text = dtrSearch["u_id"].ToString();
                    lblPoints.Text = dtrSearch["u_points"].ToString();
                    lblUserName.Text = dtrSearch["username"].ToString();
                    dtrSearch.Close();

                    Panel1.Visible = true;
                }
                else
                {
                    Panel1.Visible = false;
                    btnRedeem.Enabled = false;
                }
            }
            else
            {
                Panel1.Visible = false;
                btnRedeem.Enabled = false;
            }

            cmdSearch = new SqlCommand("SELECT * FROM Reward WHERE r_id=@rid", conn);
            cmdSearch.Parameters.AddWithValue("@rid", Request.QueryString["id"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            lblRID.Text = dtrSearch["r_id"].ToString();
            lblName.Text = dtrSearch["r_name"].ToString();
            lblName2.Text = dtrSearch["r_name"].ToString();
            lblDesc.Text = dtrSearch["r_desc"].ToString();
            lblPointsNeed.Text = dtrSearch["r_points"].ToString();
            lblQuantity.Text = dtrSearch["r_quantity"].ToString();

            dtrSearch.Close();
            conn.Close();

        }

        public string getPic()
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select r_picture From Reward Where r_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", Request.QueryString["id"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            String pic = dtrSearch["r_picture"].ToString();
            conn.Close();

            return pic;
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnRedeem_Click(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmdPoints = new SqlCommand("SELECT u_points FROM Users WHERE u_id='" + lblUserID.Text + "'", conn);
            int points = Convert.ToInt32(cmdPoints.ExecuteScalar());

            if (points > Convert.ToInt32(lblPointsNeed.Text))
            {
                SqlCommand cmdSearchTID, cmdInsert;
                cmdSearchTID = new SqlCommand("Select MAX(cl_id) from Claim", conn);
                int t_id = Convert.ToInt32(cmdSearchTID.ExecuteScalar());
                t_id++;

                cmdInsert = new SqlCommand("Insert into Claim Values(@CLID,@UID,@RID,@SID,@Status,@Date)", conn);
                cmdInsert.Parameters.AddWithValue("@CLID", t_id);
                cmdInsert.Parameters.AddWithValue("@UID", lblUserID.Text);
                cmdInsert.Parameters.AddWithValue("@RID", lblRID.Text);
                cmdInsert.Parameters.AddWithValue("@SID", Convert.ToInt32("0"));
                cmdInsert.Parameters.AddWithValue("@Status", "Reward");
                cmdInsert.Parameters.AddWithValue("@Date", DateTime.Now);
                cmdInsert.ExecuteNonQuery();

                Session["RewardReference"] = t_id;
                conn.Close();
                Response.Redirect("RewardShipping.aspx");
            }
            else
            {
                MessageBox("Sorry, your total points is not sufficient to redeem this reward.");
                conn.Close();
            }
        }
    }
}